﻿@inject IDataConfigService DataConfigService

<MudDialog>
    <DialogContent>
        <MudContainer Style="max-height: 480px; overflow-y: scroll">
            <MudForm @ref="_editForm" Model="EditData" Validation="@(_validator.ValidateValue)" ValidationDelay="0">
                <MudGrid>
                    <MudItem xs="6">
                        <MudTextField @bind-Value="EditData.RuleCode" For="@(() => EditData.RuleCode)" Label="规则编码"></MudTextField>
                    </MudItem>
                    <MudItem xs="6">
                        <MudTextField @bind-Value="EditData.RuleName" For="@(() => EditData.RuleName)" Label="规则名称"></MudTextField>
                    </MudItem>
                    @*<MudItem xs="6">
                    <MudSelect T="string" Value="EditData.DataType" ValueChanged="DataTypeChangedHandler"
                    For="@(() => EditData.DataType)" Label="数据类型" Dense="true" AnchorOrigin="Origin.BottomCenter">
                    <MudSelectItem Value="@("NONE")"></MudSelectItem>
                    <MudSelectItem Value="@("COLUMN_VARCHAR")">字符串</MudSelectItem>
                    <MudSelectItem Value="@("COLUMN_NUMBER")">数值</MudSelectItem>
                    </MudSelect>
                    </MudItem>
                    <MudItem xs="6">
                    <MudSelectList TItem="ConfigColMetaInfoResponseDto" TValue="string" Items="_colValues"
                    TextRule="@(item => item.ColumnName)"
                    ValueRule="@(item => item.ColumnCode)"
                    @bind-Value="EditData.ValueColumnMap" For="@(() => EditData.ValueColumnMap)" Label="存储字段" Dense="true" AnchorOrigin="Origin.BottomCenter">
                    </MudSelectList>
                    </MudItem>
                    <MudItem xs="6">
                    <MudSelect @bind-Value="EditData.IsRequired" For="@(() => EditData.IsRequired)" Label="必填" Dense="true" AnchorOrigin="Origin.BottomCenter">
                    <MudSelectItem Value="true">是</MudSelectItem>
                    <MudSelectItem Value="false">否</MudSelectItem>
                    </MudSelect>
                    </MudItem>
                    <MudItem xs="6">
                    <MudSelect @bind-Value="EditData.IsKey" For="@(() => EditData.IsKey)" Label="主键" Dense="true" AnchorOrigin="Origin.BottomCenter">
                    <MudSelectItem Value="true">是</MudSelectItem>
                    <MudSelectItem Value="false">否</MudSelectItem>
                    </MudSelect>
                    </MudItem>
                    <MudItem xs="6">
                    <MudSelect @bind-Value="EditData.IsVisible" For="@(() => EditData.IsVisible)" Label="可见" Dense="true" AnchorOrigin="Origin.BottomCenter">
                    <MudSelectItem Value="true">是</MudSelectItem>
                    <MudSelectItem Value="false">否</MudSelectItem>
                    </MudSelect>
                    </MudItem>
                    <MudItem xs="6">
                    <MudSelect @bind-Value="EditData.IsSearch" For="@(() => EditData.IsSearch)" Label="查询条件" Dense="true" AnchorOrigin="Origin.BottomCenter">
                    <MudSelectItem Value="true">是</MudSelectItem>
                    <MudSelectItem Value="false">否</MudSelectItem>
                    </MudSelect>
                    </MudItem>
                    <MudItem xs="6">
                    <MudSelect @bind-Value="EditData.SearchRequired" For="@(() => EditData.SearchRequired)" Label="查询必填" Dense="true" AnchorOrigin="Origin.BottomCenter">
                    <MudSelectItem Value="true">是</MudSelectItem>
                    <MudSelectItem Value="false">否</MudSelectItem>
                    </MudSelect>
                    </MudItem>
                    <MudItem xs="6">
                    <MudSelect @bind-Value="EditData.OrderBy" For="@(() => EditData.OrderBy)" Label="查询结果排序" Dense="true" AnchorOrigin="Origin.BottomCenter">
                    <MudSelectItem Value="@("None")"></MudSelectItem>
                    <MudSelectItem Value="@("Asc")">升序</MudSelectItem>
                    <MudSelectItem Value="@("Desc")">降序</MudSelectItem>
                    </MudSelect>
                    </MudItem>
                    <MudItem xs="6">
                    <MudTextRule @bind-Value="EditData.DefaultValue" For="@(() => EditData.DefaultValue)" Label="默认值"></MudTextRule>
                    </MudItem>
                    <MudItem xs="6">
                    <MudNumericRule @bind-Value="EditData.Sort" For="@(() => EditData.Sort)" Label="排序"></MudNumericRule>
                    </MudItem>
                    <MudItem xs="6">
                    <MudTextRule @bind-Value="EditData.Description" For="@(() => EditData.Description)" Label="描述"></MudTextRule>
                    </MudItem>*@
                    @*<MudItem xs="6">
                    <MudSelect T="string" Value="EditData.ControlType"
                    ValueChanged="ControlTypeChangedHandle"
                    For="@(() => EditData.ControlType)" Label="控件类型"
                    Dense="true" AnchorOrigin="Origin.BottomCenter">
                    <MudSelectItem Value="@("None")"></MudSelectItem>
                    <MudSelectItem Value="@("ControlTextBox")">文本框控件</MudSelectItem>
                    <MudSelectItem Value="@("ControlSelect")">下拉框控件</MudSelectItem>
                    <MudSelectItem Value="@("ControlCascadingSelect")">级联下拉框控件</MudSelectItem>
                    <MudSelectItem Value="@("ControlNumeric")">数值框控件</MudSelectItem>
                    <MudSelectItem Value="@("ControlDate")">日期控件</MudSelectItem>
                    </MudSelect>
                    </MudItem>
                    <CascadingValue Value="EditData" IsFixed="true">
                    <DynamicComponent Type="_selectedComponent">
                    </DynamicComponent>
                    </CascadingValue>*@
                </MudGrid>
            </MudForm>
        </MudContainer>
    </DialogContent>
    <DialogActions>
        <MudButton Variant="Variant.Filled" OnClick="Cancel">取消</MudButton>
        <MudButton Color="Color.Primary" Variant="Variant.Filled" OnClick="Submit">保存</MudButton>
    </DialogActions>
</MudDialog>


@code {
    private MudForm? _editForm = new();
    private ConfigRuleEditValidator _validator = new();


    [Parameter]
    public Guid? Key { get; set; }

    [Parameter]
    public string? ConfigCode { get; set; }

    [Parameter]
    [NotNull]
    public ConfigRuleEditRequestDto? EditData { get; set; }

    [Parameter]
    public Func<Task>? SubmitSuccessCallback { get; set; }

    [CascadingParameter]
    MudDialogInstance? MudDialog { get; set; }

    async Task Submit()
    {
        if (_editForm != null && (!string.IsNullOrEmpty(ConfigCode)))
        {
            await _editForm.Validate();

            if (_editForm.IsValid)
            {
                if (Key != null)
                {
                    await DataConfigService.UpdateDataConfigRule(Key.Value, EditData);
                }
                else
                {
                    await DataConfigService.CreateDataConfigRule(ConfigCode, EditData);
                }

                MudDialog?.Close(DialogResult.Ok(true));
            }
        }
    }

    void Cancel() => MudDialog?.Cancel();

    async Task DataTypeChangedHandler(string value)
    {
        await BindValueColumnMapSelect(value);
    }

    Task BindValueColumnMapSelect(string value)
    {
        return Task.CompletedTask;
    }

    void ControlTypeChangedHandle(string value)
    {
        if (!string.IsNullOrEmpty(value))
        {
            GetCascadingType(value);
        }
    }

    void GetCascadingType(string? controlType)
    {
        //switch (controlType)
        //{
        //    case "ControlTextBox":
        //        _selectedComponent = typeof(RuleTextBoxPartial);
        //        break;
        //    case "ControlSelect":
        //        _selectedComponent = typeof(RuleSelectPartial);
        //        break;
        //    case "ControlNumeric":
        //        _selectedComponent = typeof(RuleNumericPartial);
        //        break;
        //    case "ControlDate":
        //        _selectedComponent = typeof(RuleDatePartial);
        //        break;
        //    default:
        //        _selectedComponent = typeof(RuleEmptyPartial);
        //        break;
        //}
    }

    protected override async Task OnParametersSetAsync()
    {
        EditData = new ConfigRuleEditRequestDto();
        if (Key != null)
        {
            var result = await DataConfigService.GetDataConfigRule(Key.Value);
            if (result.Succeeded && result.Data != null)
            {

                EditData.RuleCode = result.Data.RuleCode;
                EditData.RuleName = result.Data.RuleName;
                EditData.Sort = result.Data.Sort;
                EditData.Description = result.Data.Description;
            }
        }
    }
}
